package com.authorization.project.authorization.controller;

import java.util.List;

import com.authorization.common.utils.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.authorization.framework.aspectj.lang.annotation.Log;
import com.authorization.framework.aspectj.lang.enums.BusinessType;
import com.authorization.project.authorization.domain.AuthorizationToken;
import com.authorization.project.authorization.service.IAuthorizationTokenService;
import com.authorization.framework.web.controller.BaseController;
import com.authorization.framework.web.domain.AjaxResult;
import com.authorization.common.utils.poi.ExcelUtil;
import com.authorization.framework.web.page.TableDataInfo;

/**
 * 授权tokenController
 * 
 * @author 李晓雷
 * @date 2020-05-09
 */
@RestController
@RequestMapping("/authorization/token")
public class AuthorizationTokenController extends BaseController
{
    @Autowired
    private IAuthorizationTokenService authorizationTokenService;

    /**
     * 查询授权token列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:token:list')")
    @GetMapping("/list")
    public TableDataInfo list(AuthorizationToken authorizationToken)
    {
        startPage();
        List<AuthorizationToken> list = authorizationTokenService.selectAuthorizationTokenList(authorizationToken);
        return getDataTable(list);
    }

    /**
     * 导出授权token列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:token:export')")
    @Log(title = "授权token", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AuthorizationToken authorizationToken)
    {
        List<AuthorizationToken> list = authorizationTokenService.selectAuthorizationTokenList(authorizationToken);
        ExcelUtil<AuthorizationToken> util = new ExcelUtil<AuthorizationToken>(AuthorizationToken.class);
        return util.exportExcel(list, "token");
    }

    /**
     * 获取授权token详细信息
     */
    @PreAuthorize("@ss.hasPermi('authorization:token:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(authorizationTokenService.selectAuthorizationTokenById(id));
    }

    /**
     * 新增授权token
     */
    @PreAuthorize("@ss.hasPermi('authorization:token:add')")
    @Log(title = "授权token", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuthorizationToken authorizationToken)
    {
        return toAjax(authorizationTokenService.insertAuthorizationToken(authorizationToken));
    }

    /**
     * 修改授权token
     */
    @PreAuthorize("@ss.hasPermi('authorization:token:edit')")
    @Log(title = "授权token", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuthorizationToken authorizationToken)
    {
        return toAjax(authorizationTokenService.updateAuthorizationToken(authorizationToken));
    }

    /**
     * 删除授权token
     */
    @PreAuthorize("@ss.hasPermi('authorization:token:remove')")
    @Log(title = "授权token", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(authorizationTokenService.deleteAuthorizationTokenByIds(ids));
    }

    /**
     * 创建Token
     * @return
     */
    @GetMapping(value = "/createToken")
    public AjaxResult createToken()
    {
        return AjaxResult.success("ok", IdUtils.fastUUID());
    }
}
